/**
 * Created by lyc on 2017/3/28.
 */
'use strict'
import * as types from '../mutations-type'
import axios from 'axios'
import api from '../api'
const state = {
  dialogVisible: false,
  taking_tableData: [],
  checkNum: 0
}

const mutations = {
  [types.TAKING_SHOW_FORM] (state) {
    state.dialogVisible = true
  },
  [types.TAKING_CLOSE_FORM] (state) {
    state.dialogVisible = false
  },
  [types.TAKING_REQ_LIST] (state, action) {
    state.taking_tableData = action.result.data
  },
  [types.TAKING_CHECK_NUM] (state, action) {
    state.checkNum = action.num
  }
}
const getters = {
  taking_tableData: state => state.taking_tableData
}
const actions = {
  [types.TAKING_SHOW_FORM] (context) {
    context.commit(types.TAKING_SHOW_FORM)
  },
  [types.TAKING_CLOSE_FORM] (context) {
    context.commit(types.TAKING_CLOSE_FORM)
  },
  [types.TAKING_REQ_LIST] ({commit}, params) {
    axios.get(api.takingList + params)
      .then(response => {
        const result = response.data
        if (result.success) {
          commit(types.TAKING_CHECK_NUM, {
            num: 0
          })
          commit(types.TAKING_REQ_LIST, {
            result: response
          })
        } else {
          this.$message.error({
            title: '错误',
            message: result.message
          })
        }
      }, (err) => {
        commit(types.TAKING_REQ_LIST, {
          result: {
            data: err.response.data
          }
        })
      })
  },
  [types.TAKING_CHECK_NUM] ({commit}, num) {
    commit(types.TAKING_CHECK_NUM, {
      num: num
    })
  }
}

export default {
  getters,
  state,
  actions,
  mutations
}
